package com.jay.media.music.ui.music

import android.media.MediaMetadata
import android.media.browse.MediaBrowser
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jay.base.ui.BaseFragment
import com.jay.media.kit.LogHelper
import com.jay.media.music.ui.HomeActivity
import com.jay.media.music.ui.R
import com.jay.media.music.ui.manager.DefaultMusicListener
import com.jay.media.music.ui.manager.MusicClientManager

/**
 * Create by xuzhou4
 * Date 2021/11/11
 */
class MusicBrowserFragment : BaseFragment(), MediaAdapter.OnItemClickListener {

    companion object {
        fun createInstance(
            mediaId: String?,
            subTitle: String?,
            onItemClickListener: MediaAdapter.OnItemClickListener
        ): MusicBrowserFragment {
            val mediaBrowserFragment = MusicBrowserFragment()
            val bundle = Bundle(8)
            bundle.putString(MediaMetadata.METADATA_KEY_MEDIA_ID, mediaId)
            bundle.putString(MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE, subTitle)
            mediaBrowserFragment.arguments = bundle
            mediaBrowserFragment.setOnItemClickListener(onItemClickListener)
            return mediaBrowserFragment
        }
    }

    private fun setOnItemClickListener(onItemClickListener: MediaAdapter.OnItemClickListener) {
        this.mOnItemClickListener = onItemClickListener
    }

    private lateinit var mMediaId: String
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mErrorTextView: TextView
    private lateinit var mMediaAdapter: MediaAdapter
    private var mOnItemClickListener: MediaAdapter.OnItemClickListener? = null

    private var mHandler: Handler = Handler(Looper.getMainLooper())
    private var mMediaListener = object : DefaultMusicListener() {
        override fun onChildrenLoaded(
            parentId: String,
            children: MutableList<MediaBrowser.MediaItem>
        ) {
            LogHelper.d(
                TAG,
                "onChildrenLoaded() called with: mMediaId = $mMediaId, parentId = $parentId, children = $children"
            )
            if (parentId == mMediaId) {
                mHandler.post {
                    mMediaAdapter.list = children
                    mMediaAdapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun layoutId(): Int = R.layout.fragment_list

    override fun initView(view: View) {
        mRecyclerView = requireView().findViewById(R.id.recyclerView)
        mErrorTextView = requireView().findViewById(R.id.error_message)

        mRecyclerView.layoutManager = LinearLayoutManager(context)
        mMediaAdapter = MediaAdapter()
        mMediaAdapter.mOnItemClickListener = this
        mMediaAdapter.itemCount
        mRecyclerView.adapter = mMediaAdapter

        val homeActivity = mActivity as HomeActivity
        homeActivity.updateToolbarTitle(getTitle())
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume() called")
        mMediaId = getMediaId()
        MusicClientManager.getClient().registerMediaListener(mMediaListener)
        MusicClientManager.getClient().subscribe(mMediaId)
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause() called")
        MusicClientManager.getClient().unregisterMediaListener(mMediaListener)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView() called")
    }

    private fun getTitle(): String {
        val media = getString(R.string.menu_music)
        return arguments?.getString(
            MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE,
            media
        )
            ?: media
    }

    fun getMediaId(): String {
        val root = MusicClientManager.getClient().mRoot ?: ""
        return return arguments?.getString(
            MediaMetadata.METADATA_KEY_MEDIA_ID, root
        )
            ?: root
    }

    override fun onItemClick(view: View, item: MediaBrowser.MediaItem) {
        mOnItemClickListener?.onItemClick(view, item)
    }

    override fun onItemMenuClick(view: View, position: Int, item: MediaBrowser.MediaItem) {
        mOnItemClickListener?.onItemMenuClick(view, position, item)
    }
}